﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace Ccit.Utils
{
    /// <summary>
    /// 登陆用户类
    /// </summary>
    public class LoginUser
    {
        #region private attributes
        private string _pOrganId;
        private string _pOrganName;
        private string _pLgUserId;
        private string _pLgUserName;
        private string _pDeptId;
        private string _pDeptName;
        private string _pPosId;
        private string _pPosName;
        private string _pClassId;
        private string _pClassName;
        private string _pUserType;  //登陆用户类型 0:普通用户 1:机构管理员 2:超级管理员     
        private string _pOperMenuIds;  //登陆用户所具有的操作权限菜单Ids
        #region XML 结构
        /******************************************************
            <LOGINUSER>
                <USERID></USERID>
                <USERNAME></USERNAME>
                <USERTYPE></USERTYPE>
                <ORGANID></ORGANID>
                <ORGANNAME></ORGANNAME>
                <DEPTID></DEPTID>
                <DEPTNAME></DEPTNAME>
                <POSID></POSID>
                <POSNAME></POSNAME>                
                <CLASSID></CLASSID>
                <CLASSNAME></CLASSNAME>
            </LOGINUSER>             
        *****************************************************/
        #endregion
        private string _pLoginUserXML;
        #endregion

        #region public attributes
        /// <summary>
        /// 当前登陆用户所在机构ID
        /// </summary>
        public string OrganId
        {
            get { return ((null != _pOrganId) ? _pOrganId : ""); }
        }

        /// <summary>
        /// 当前登陆用户所在机构名称 
        /// </summary>
        public string OrganName
        {
            get { return ((null != _pOrganName) ? _pOrganName : ""); }
        }

        /// <summary>
        /// 当前登陆用户ID
        /// </summary>
        public string UserId
        {
            get { return ((null != _pLgUserId) ? _pLgUserId : ""); }
        }

        /// <summary>
        /// 当前登陆用户名称
        /// </summary>
        public string UserName
        {
            get { return ((null != _pLgUserName) ? _pLgUserName : ""); }
        }

        /// <summary>
        /// 当前登陆用户所属部门ID
        /// </summary>
        public string DeptId
        {
            get { return ((null != _pDeptId) ? _pDeptId : ""); }
        }

        /// <summary>
        /// 当前登陆用户所属部门名称
        /// </summary>
        public string DeptName
        {
            get { return ((null != _pDeptName) ? _pDeptName : ""); }
        }

        /// <summary>
        /// 当前登陆用户所处职位ID
        /// </summary>
        public string PosId
        {
            get { return ((null != _pPosId) ? _pPosId : ""); }
        }

        /// <summary>
        /// 当前登陆用户所处职位名称
        /// </summary>
        public string PosName
        {
            get { return ((null != _pPosName) ? _pPosName : ""); }
        }

        /// <summary>
        /// 当前登陆用户所处级别ID
        /// </summary>
        public string ClassId
        {
            get { return ((null != _pClassId) ? _pClassId : ""); }
        }

        /// <summary>
        /// 当前登陆用户所处级别名称
        /// </summary>
        public string ClassName
        {
            get { return ((null != _pClassName) ? _pClassName : ""); }
        }

        /// <summary>
        /// 当前登陆用户用户类型
        /// </summary>
        public string UserType
        {
            get { return ((null != _pUserType) ? _pUserType : ""); }
        }

        /// <summary>
        /// 当前登陆用户的菜单操作权限集合
        /// </summary>
        public string OperMenuIds
        {
            get { return ((null != _pOperMenuIds) ? _pOperMenuIds : ""); }
            set { _pOperMenuIds = value; }
        }

        /// <summary>
        /// 当前登陆用户属性XML结构
        /// </summary>
        public string LoginUserXML
        {
            get { return ((null != _pLoginUserXML) ? _pLoginUserXML : ""); }
            set
            {
                _pLoginUserXML = value;
                setAttributesByXML();
            }
        }
        #endregion

        #region constructor
        /// <summary>
        /// 构造函数
        /// </summary>
        public LoginUser() { }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="strLoginUserXML"></param>
        public LoginUser(string strLoginUserXML)
        {
            LoginUserXML = strLoginUserXML;

        }

        #region private methods
        private void setAttributesByXML()
        {
            if (string.IsNullOrEmpty(_pLoginUserXML)) return;

            _pLgUserId = getElementValueByName("USERID");
            _pLgUserName = getElementValueByName("USERNAME");
            _pUserType = getElementValueByName("USERTYPE");
            _pOrganId = getElementValueByName("ORGANID");
            _pOrganName = getElementValueByName("ORGANNAME");
            _pDeptId = getElementValueByName("DEPTID");
            _pDeptName = getElementValueByName("DEPTNAME");
            _pPosId = getElementValueByName("POSID");
            _pPosName = getElementValueByName("POSNAME");
            _pClassId = getElementValueByName("CLASSID");
            _pClassName = getElementValueByName("CLASSNAME");
        }

        private string getElementValueByName(string sElementName)
        {
            string sRetValue = "";
            XElement XeLoginUser = XElement.Parse(_pLoginUserXML);

            var vElementValues = from GTag in XeLoginUser.Elements(sElementName) select GTag;
            if (null != vElementValues)
            {
                foreach (var vElement in vElementValues)
                {
                    sRetValue = vElement.Value;
                    break;
                }
            }

            XeLoginUser = null;
            return sRetValue;
        }
        #endregion

        #endregion

    }
}
